package com.jxzyk.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.core.entity.I_List;
import com.jxzyk.App;
import com.jxzyk.R;
import com.jxzyk.adapter.GroupAdapter;
import com.jxzyk.entity.CourseType;
import com.jxzyk.util.NumberWatcher;
import com.jxzyk.util.OnNumberWatcher;

public class TitleLayout extends LinearLayout {
	private Context mContext;
	private TextView tv_title;
	private PopupWindow popupWindow;
	private ListView lv_group;
	private View view;

	// private List<String> groups;
	// private List<I_List> data;
	private ImageView img_search, img_left;

	public interface OnPopupWindowListener {
		void onClick(int position);
	}

	public TitleLayout(Context context) {
		super(context);
		init(context);
	}

	public TitleLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public TitleLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private LinearLayout ly1, ly2;
	public EditText et_search;
	private TextView tv_search, tv_right;
	private boolean isSearch = false;

	public boolean isSearch() {
		return isSearch;
	}

	private void init(Context context) {
		this.mContext = context;
		LayoutInflater.from(context).inflate(R.layout.common_title, this);
		tv_title = (TextView) findViewById(R.id.common_title_tv);
		img_search = (ImageView) findViewById(R.id.common_img_search);
		ly1 = (LinearLayout) findViewById(R.id.common_title_ly1);
		ly2 = (LinearLayout) findViewById(R.id.common_title_ly2);
		et_search = (EditText) findViewById(R.id.common_et_search);
		tv_search = (TextView) findViewById(R.id.common_et_search_ok);
		tv_right = (TextView) findViewById(R.id.common_tv_right);
		img_left = (ImageView) findViewById(R.id.common_title_img_left);
		et_search.addTextChangedListener(new NumberWatcher(4,
				new OnNumberWatcher() {

					@Override
					public void onFinish() {

					}

					@Override
					public void onProcess(int process) {
						if (process > 0) {
							isSearch = true;
							tv_search.setText("确认");
						} else {
							isSearch = false;
							tv_search.setText("取消");
						}
					}
				}));
	}

	public void setOnRightListener(String right, OnClickListener listener) {
		tv_right.setVisibility(View.VISIBLE);
		tv_right.setText(right);
		tv_right.setOnClickListener(listener);
	}

	public void setOnLeftListener(int imgId, OnClickListener listener) {
		img_left.setVisibility(View.VISIBLE);
		img_left.setImageResource(imgId);
		img_left.setOnClickListener(listener);
	}

	public void setVisibility(int type) {
		switch (type) {
		case 1:
			ly1.setVisibility(View.VISIBLE);
			ly2.setVisibility(View.GONE);
			break;
		case 2:
			ly1.setVisibility(View.GONE);
			ly2.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}

	}

	public void setTtile(String title) {
		tv_title.setVisibility(View.VISIBLE);
		tv_title.setText(title);
	}

	public void setSearchListener(OnClickListener listener) {
		if (ly1.getVisibility() == View.VISIBLE) {
			img_search.setVisibility(View.VISIBLE);
			img_search.setOnClickListener(listener);
		}
		if (ly2.getVisibility() == View.VISIBLE) {
			tv_search.setOnClickListener(listener);
		}
	}

	public void setOnRightListener(int resId, OnClickListener listener) {
		img_search.setVisibility(View.VISIBLE);
		img_search.setImageResource(resId);
		img_search.setOnClickListener(listener);
	}

	public void setPopupWindowListener(final List<String> datas,
			final OnPopupWindowListener listener) {
		Drawable drawable = getResources().getDrawable(R.drawable.ic_pull);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		tv_title.setCompoundDrawables(null, null, drawable, null);

		tv_title.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					showWindow(datas, v, listener);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * 显示
	 * 
	 * @param datas
	 * 
	 * @param parent
	 * @param listener
	 * @throws Exception
	 */
	private void showWindow(List<String> datas, View parent,
			final OnPopupWindowListener listener) throws Exception {
		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.group_list, null);
			lv_group = (ListView) view.findViewById(R.id.lvGroup);

			GroupAdapter groupAdapter = new GroupAdapter(getContext(), datas);
			lv_group.setAdapter(groupAdapter);

			int width = getWidth();
			int height = getWidth() * 3 / 5;
			// 创建一个PopuWidow对象
			popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
		}

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);

		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		WindowManager windowManager = (WindowManager) getContext()
				.getSystemService(Context.WINDOW_SERVICE);
		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半
		int xPos = windowManager.getDefaultDisplay().getWidth() / 2
				- popupWindow.getWidth() / 2;

		Log.i("coder", "windowManager.getDefaultDisplay().getWidth()/2:"
				+ windowManager.getDefaultDisplay().getWidth() / 2);
		//
		Log.i("coder", "popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2);

		Log.i("coder", "xPos:" + xPos);

		popupWindow.showAsDropDown(parent, xPos, 10);

		lv_group.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				listener.onClick(position);
				if (popupWindow != null) {
					popupWindow.dismiss();
				}
			}
		});
	}
}
